package net.scribblemedia.candle.model;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.PERSIST;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class TrainableNetwork {

    @Lob
    private byte[] trainedNetwork;
	
	@OneToMany(mappedBy="trainableNetwork", cascade=ALL)
	private List<LayerConfiguration> layers;
	
	@ManyToMany(mappedBy="trainableNetworks", cascade={PERSIST, MERGE}, fetch=FetchType.EAGER)
	private Set<TrainingPlan> trainingPlans;
	
	public void addLayer(LayerConfiguration layerConfiguration) {
		if (layers == null) {
			layers = new ArrayList<LayerConfiguration>();
		}
		
		layers.add(layerConfiguration);
		layerConfiguration.setTrainableNetwork(this);
	}
	
	public void addTrainingPlan(TrainingPlan trainingPlan) {
		if (trainingPlans == null) {
			trainingPlans = new HashSet<TrainingPlan>();
		}
		
		trainingPlans.add(trainingPlan);
		trainingPlan.addTrainableNetworkInternal(this);
	}
	
}
